package com.example.code.robot;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class RobotDemo {

    private Robot robot = null;

    public RobotDemo() {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
    /**
     * 可以弹出QQ
     */
    public void keyBoardDemo() {
        robot.keyPress(KeyEvent.VK_ALT);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_Z);
        robot.keyRelease(KeyEvent.VK_Z);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_ALT);
    }
    /**
     * 前提是有个最大化的窗口，功能是移动到标题栏，然后拖拽到600,600的位置
     */
    public void mouseDemo() {
        robot.mouseMove(80, 10);
        robot.mousePress(KeyEvent.BUTTON1_MASK);
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        robot.mouseMove(600, 600);
        robot.mouseRelease(KeyEvent.BUTTON1_MASK);
    }

    /**
     * 截屏
     */
    public static void screen_shot() throws AWTException {
        screen_shot("E:\\screenRect2.png");
    }
    /**
     * 截屏
     */
    public static void screen_shot(String path) throws AWTException {
        Robot robot = new Robot();
        Rectangle screenRect = new Rectangle(0, 0, 400, 800);//这里可以自己设置图片范围

        //截图
        BufferedImage bufferedImage = robot.createScreenCapture(screenRect);

        //保存截图
        File file = new File(path);
        try {
            ImageIO.write(bufferedImage, "png", file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void m () throws AWTException {
        Robot robot = new Robot();
        robot.mouseMove(400,800);


    }
    /**
     * @param args
     */
    public static void main(String[] args) throws AWTException {
//        RobotDemo demo = new RobotDemo();
//        demo.keyBoardDemo();
//        demo.mouseDemo();

        m();


    }

}
